<template>
  <div class="acc_list_box">
    <!-- 头部搜索 -->
    <el-form :inline="true" :model="listFrom" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="listFrom.name" placeholder="请输入姓名进行查询" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-popconfirm title="确定删除所选账号吗?" @confirm="onDelete()">
          <template #reference>
            <el-button type="info">删除选中</el-button>
          </template>
        </el-popconfirm>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      border
      stripe
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column property="account" label="账号" align="center" />
      <el-table-column property="purview" label="权限" align="center" />
      <el-table-column property="name" label="姓名" align="center" />
      <el-table-column property="sex" label="性别" align="center" />
      <el-table-column property="phone" label="手机号" align="center" />
      <el-table-column label="账号操作" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            :icon="Document"
            @click="DetailAccount(scope.row.id)"
          />
          <el-popconfirm
            title="确定删除该账号吗?"
            @confirm="deleteAccount(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" :icon="Delete" />
            </template>
          </el-popconfirm>

          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="editAccount(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="fenyeqi">
      <el-pagination
        v-model:current-page="listFrom.page"
        v-model:page-size="listFrom.size"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>
    <!-- 详情页弹框 -->
    <el-dialog
      v-model="dialogVisible"
      title="医务账号详情"
      width="35%"
      align-center
    >
      <el-form :model="form" label-width="120px">
        <el-form-item label="医务账号">
          <el-input v-model="form.account" disabled />
        </el-form-item>
        <el-form-item label="权限">
          <el-input v-if="isShow" v-model="form.purview" disabled />
          <el-select v-if="isShow2" v-model="form.purview" placeholder="">
            <el-option label="医生" value="医生" />
            <el-option label="护士" value="护士" />
            <el-option label="医务管理员" value="医务管理员" />
            <el-option label="实习助理" value="实习助理" />
            <el-option label="药房" value="药房" />
          </el-select>
        </el-form-item>
        <el-form-item label="账号密码">
          <el-input v-if="isShow" v-model="form.password" disabled />
          <el-input v-if="isShow2" v-model="form.password" />
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.idcard" disabled />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-if="isShow" v-model="form.phone" disabled />
          <el-input v-if="isShow2" v-model="form.phone" />
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.sex" disabled />
        </el-form-item>
        <el-form-item>
          <img id="imgUrl" v-if="isShow" :src="form.head" alt="" />
          <el-upload
            class="avatar-uploader"
            action="http://129.211.169.131:21001/users/uploadheader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            v-if="isShow2"
          >
            <img class="avatar" v-if="imageUrl" :src="imageUrl" alt="" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button
            v-if="isShow"
            type="primary"
            @click="dialogVisible = false"
          >
            确定
          </el-button>
          <el-button v-if="isShow2" type="primary" @click="confirmEditing">
            确认编辑
          </el-button>
          <el-button v-if="isShow2" type="info" @click="dialogVisible = false">
            取消
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  usersListReq,
  usersSearchReq,
  usersDelReq,
  usersBatchdelReq,
  usersGetbyidReq,
  usersEditReq,
} from "@/api/user.api";
import { watchEffect, reactive, ref } from "vue";
import { Delete, Edit, Document, Plus } from "@element-plus/icons-vue";
import { ElTable, ElMessage, UploadProps } from "element-plus";
const listFrom = reactive({
  page: 1,
  size: 10,
  name: "",
});
// 详情表单表单数据
const form = reactive({
  account: "",
  purview: "",
  password: "",
  sex: "",
  idcard: "",
  phone: "",
  head: "",
  id: "",
});
// 表格数据
const tableData = ref([]);
//删除选中的数组
const ids = ref<any>([]);
// 总条数
const total = ref(0);
// 图片值
const imageUrl = ref("");
//开关
const dialogVisible = ref(false);
const isShow = ref(false);
const isShow2 = ref(false);
// 点击查询
const onSubmit = async () => {
  if (listFrom.name == "") {
    ElMessage.error("请输入姓名！");
    return;
  }
  const res = await usersSearchReq(listFrom);
  tableData.value = res.data.data;
  total.value = res.data.total;
  if (tableData.value.length == 0) {
    ElMessage.error("没有该姓名的数据！");
  }
};
// 单个删除
const deleteAccount = async (id) => {
  const res = await usersDelReq({ id });
  if (res.data.code == 1) {
    ElMessage({
      message: res.data.msg,
      type: "success",
    });
  } else {
    ElMessage.error(res.data.msg);
  }
  addList();
};
//单选框选中值
const handleSelectionChange = (val) => {
  ids.value = JSON.parse(JSON.stringify(val)).map((item) => item.id);
};
//删除选中
const onDelete = async () => {
  if (ids.value.length == 0) {
    ElMessage.error("请选择要删除的账号！");
    return;
  }
  ids.value = JSON.stringify(ids.value);
  const res = await usersBatchdelReq({ ids: ids.value });
  if (res.data.code == 1) {
    ElMessage({
      message: res.data.msg,
      type: "success",
    });
  } else {
    ElMessage.error(res.data.msg);
  }
  addList();
};
//点击查看详情
const DetailAccount = async (id) => {
  const res = await usersGetbyidReq({ id });
  let obj = res.data.data[0];
  form.account = obj.account;
  form.purview = obj.purview;
  form.password = obj.password;
  form.idcard = obj.idcard;
  form.phone = obj.phone;
  form.sex = obj.sex;
  form.head = obj.headurl;
  dialogVisible.value = true;
  isShow.value = true;
  isShow2.value = false;
};
// 点击编辑
const editAccount = async (id) => {
  const res = await usersGetbyidReq({ id });
  let obj = res.data.data[0];
  form.account = obj.account;
  form.purview = obj.purview;
  form.password = obj.password;
  form.idcard = obj.idcard;
  form.phone = obj.phone;
  form.sex = obj.sex;
  form.head = obj.headurl;
  form.id = obj.id;
  imageUrl.value = obj.headurl;
  dialogVisible.value = true;
  isShow.value = false;
  isShow2.value = true;
};
// 点击确认编辑
const confirmEditing = async () => {
  const res = await usersEditReq(form);
  // console.log(res);
  if (res.data.code == 1) {
    ElMessage({
      message: res.data.msg,
      type: "success",
    });
    dialogVisible.value = false;
  } else {
    ElMessage.error(res.data.msg);
  }
  addList();
};
//图片上传成功的钩子
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  //   console.log(uploadFile.raw.name, "9999");
  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
  form.head = uploadFile.raw.name;
};
//请求列表数据
const addList = async () => {
  const res = await usersListReq(listFrom);
  tableData.value = res.data.data;
  total.value = res.data.total;
};
// 监听数据列表发生改变
watchEffect(addList);
</script>

<style scoped lang="scss">
.acc_list_box {
  width: 100%;
  height: 100%;
  .fenyeqi {
    margin-top: 20px;
  }
  #imgUrl {
    width: 120px;
    height: 120px;
    border-radius: 15px;
  }
  .avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
// ::-webkit-scrollbar {width: 0;
//  }
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
